const pageComponentSetting = {
	/*
	'pages/home/info/info': {
		toast(){...},
		confirm(){...}
	},
	'pages/home/info/info': {
		toast(){...},
		confirm(){...}
	},
	'pages/home/info/info': {
		toast(){...},
		confirm(){...}
	},
	...
	*/
};
const updatePageSetting = {
	/**
	 'pages/home/info/info': true,
	 'pages/home/info/info': false,
	 */
};

function setComponentHandler(key, handler) {
	const routes = getCurrentPages();
	const last = routes[routes.length - 1];
	if (last) {
		const target = pageComponentSetting[last.route];
		if (target) {
			target[key] = handler;
		} else {
			pageComponentSetting[last.route] = {
				[key]: handler
			};
		}
	}
}
let toastInter = null;
export default function(obj) {
	return Object.assign({
		tabBarHeight: 56, //自定义tabbar高度
		setToast(showToastHandler) {
			setComponentHandler("toast", showToastHandler);
		},
		setConfirm(confirmHandler) {
			setComponentHandler("confirm", confirmHandler);
		},
		toast(mes, isDelay = false, icon = 'none') {
			const routes = getCurrentPages();
			const last = routes[routes.length - 1];
			const target = pageComponentSetting[last.route];
			if (target && target.toast) {
				target.toast(mes);
			} else {
				if (isDelay) {
					toastInter && clearTimeout(toastInter);
					toastInter = setTimeout(() => {
						uni.showToast({
							icon,
							title: mes,
							duration: 2000,
						});
					}, 200)
				} else {
					uni.showToast({
						icon,
						title: mes,
						duration: 2000,
					});
				}
			}
		},
		/**
		 * @param {String} mes 提示内容
		 * @param {Object} options
		 * @var {String} title 标题
		 * @var {Function} ok 确定回调
		 * @var {Function} cancel取消回调
		 * 其它查看confirm-dialog.vue组件
		 */
		confirm(mes, options) {
			const routes = getCurrentPages();
			const last = routes[routes.length - 1];
			const target = pageComponentSetting[last.route];
			if (target) {
				target.confirm(mes, options);
			}
		},
		//当前a页面数据变了 b页面需跟着变
		setUpdatePage(pageRoute, needUpdate = true) {
			updatePageSetting[pageRoute] = needUpdate;
		},
		resetCurPageUpdate() {
			const pages = getCurrentPages();
			const curPage = pages[pages.length - 1];
			updatePageSetting[curPage.route] = false;
		},
		//当前页面是否需要更新 在onShow中调用
		curPageNeedUpdate(autoReset = true) {
			const pages = getCurrentPages();
			const curPage = pages[pages.length - 1];
			// console.log("=============>curPageNeedUpdate",curPage.route);
			const needUpdate = updatePageSetting[curPage.route];
			if (autoReset) {
				updatePageSetting[curPage.route] = false;
			}
			return needUpdate;
		}
	}, obj || {});
}